<?php

Yii::import('application.extensions.PHPMailer.AqSendMail');

class ComplianceController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Compliance'),
        ));
    }

    public function actionCreate() {
        $model = new Compliance;
         $pass = '';
        if (isset($_POST['Compliance'])) {
            $model->attributes = $_POST['Compliance'];
            $password = substr(time(), 5, 8) . substr($_POST['Compliance']['email_address_of_the_head'], 3, 5) . substr($_POST['Compliance']['mobile_phone_of_the_head'], 4, 7);
            $model->setAttribute('password', $password);   
            $pass = 1;
            if ($model->validate()) {
              
                $model->save();
                Yii::app()->session['compliance_id'] = $model->id;
                Yii::app()->user->setFlash('Password', 'Please check your mail and mobile no.i have send a password.');
                $adminSubject = 'Password for Update Institute information';
                $adminTpl = 'sendCollegePassword.php';
                $this->sendEmail($_POST['Compliance']['name_of_the_head_of_the_institution'], $password, $_POST['Compliance']['email_address_of_the_head'], $adminSubject, $adminTpl);
                $this->render('_password_text');
            }
        }

        $this->render('_createCompliance', array(
            'model' => $model, 'pass' => $pass
        ));
    }

    public function actioncomplianceUpdate() {
        if (!empty(Yii::app()->session['compliance_id'])) {
            $model = $this->loadModel(Yii::app()->session['compliance_id'], 'Compliance');
            if (isset($_POST['Compliance'])) {
                $model->attributes = $_POST['Compliance'];
                if ($model->validate()) {
                    $model->save();
                    Yii::app()->session['compliance_id'] = '';
                    $this->render('_thankyou');
                }
            }

            $this->render('update', array(
                'model' => $model,
            ));
        } else {
            throw new CHttpException(404, 'The specified request cannot be found.');
        }
    }

    public function actionpassword() {
        if (!empty(Yii::app()->session['compliance_id'])) {
            $model = $this->loadModel(Yii::app()->session['compliance_id'], 'Compliance');
            $this->render('_collegePassword', array(
                'model' => $model,
            ));
        } else
            throw new CHttpException(404, 'The specified post cannot be found.');
    }

    public function actioncheckPassword() {
        if (!empty($_POST['password'])) {
            $password = Compliance::model()->findByAttributes(array('id' => Yii::app()->session['compliance_id'], 'password' => $_POST['password']));
            if (count($password) > 0) {
                echo 1;
            } else {
                echo 'Please enter currect password.';
            }
        }
    }

    public function actionUpdateCollgeInfor() {
        if (isset($_POST['Compliance']['name_of_the_head_of_the_institution']) AND ! empty($_POST['Compliance']['name_of_the_head_of_the_institution'])) {
            $password = substr($_POST['Compliance']['name_of_the_head_of_the_institution'], 0, 2) . substr($_POST['Compliance']['email_address_of_the_head'], 3, 5) . substr($_POST['Compliance']['mobile_phone_of_the_head'], 4, 7);
            College::model()->updateByPk(Yii::app()->session['collge_id'], array(
                "name_of_the_head_of_the_institution" => $_POST['Compliance']['name_of_the_head_of_the_institution'],
                "email_address_of_the_head" => $_POST['Compliance']['email_address_of_the_head'], "mobile_phone_of_the_head" => $_POST['Compliance']['mobile_phone_of_the_head'], 'password' => $password));
            $adminSubject = 'Password for Update Institute information';
            $adminTpl = 'sendCollegePassword.php';
            $this->sendEmail($_POST['Compliance']['name_of_the_head_of_the_institution'], $password, $_POST['Compliance']['email_address_of_the_head'], $adminSubject, $adminTpl);
            echo "Please check your mail.your password has been send on mail and your mobile no. ";
        }
    }

    public function actionIndex() {
        $model = new Compliance;
        if (isset($_POST['State']['id'])) {
            $compliance = Compliance::model()->findByAttributes(array('state_id' => $_POST['State']['id'], 'id' => $_POST['Compliance']['id']));
            if (!empty($compliance)) {
                Yii::app()->session['compliance_id'] = $compliance->id;
                $this->redirect(array('/compliance/password'));
            } else {
                Yii::app()->user->setFlash('noresult', 'No record found.please click on i can not found link and create a compliance.');
            }
        }
        $this->render('_compliance_search', array(
            'model' => $model,
        ));
    }

    public function actionfindcompliance() {
        $select = '';
        $data = Compliance::model()->findAllByAttributes(array('state_id' => (int) $_POST ['State']['id']));
        $data = CHtml::listData($data, 'id', 'name_of_the_institution');
        if (empty($data)) {
            $select = CHtml::tag('option', array('value' => ''), 'Select Institute name', true);
        }
        foreach ($data as $value => $name) {
            $select.= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        echo $select;
        die;
    }

    public function actioncollgeAddress() {
        $select = '';
        if (!empty($_POST['Compliance_id'])) {
            $data = Compliance::model()->findAllByAttributes(array('id' => (int) $_POST ['Compliance_id']));
            $data = CHtml::listData($data, 'id', 'address');
        }
        if (empty($data)) {
            $select = CHtml::tag('option', array('value' => ''), 'Select Institute address', true);
        } else {
            foreach ($data as $value => $name) {
                $select.= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
            }
        }
        echo $select;
        die;
    }

    public function sendEmail($userName, $password, $to, $subject, $tpl) {
        $emalDir = aqConfig::getEmailDir();
        $mail['to'] = $to;
        $mail['subject'] = $subject;
        $mail['msgtpl'] = $emalDir . $tpl;
        $mailReplaceArr = array('$USER_NAME' => $userName, '$PASSWORD' => $password);

        AqSendMail::SendMail($mail, $mailReplaceArr);
    }

}
